{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Number of Students Unable to Eat Lunch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #stack #queue #array #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #栈 #队列 #数组 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countStudents"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #无法吃午餐的学生数量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>学校的自助午餐提供圆形和方形的三明治，分别用数字 <code>0</code> 和 <code>1</code> 表示。所有学生站在一个队列里，每个学生要么喜欢圆形的要么喜欢方形的。<br>\n",
    "餐厅里三明治的数量与学生的数量相同。所有三明治都放在一个 <strong>栈</strong> 里，每一轮：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>如果队列最前面的学生 <strong>喜欢</strong> 栈顶的三明治，那么会 <strong>拿走它</strong> 并离开队列。</li>\n",
    "\t<li>否则，这名学生会 <strong>放弃这个三明治</strong> 并回到队列的尾部。</li>\n",
    "</ul>\n",
    "\n",
    "<p>这个过程会一直持续到队列里所有学生都不喜欢栈顶的三明治为止。</p>\n",
    "\n",
    "<p>给你两个整数数组 <code>students</code> 和 <code>sandwiches</code> ，其中 <code>sandwiches[i]</code> 是栈里面第 <code>i<sup>​​​​​​</sup></code> 个三明治的类型（<code>i = 0</code> 是栈的顶部）， <code>students[j]</code> 是初始队列里第 <code>j<sup>​​​​​​</sup></code> 名学生对三明治的喜好（<code>j = 0</code> 是队列的最开始位置）。请你返回无法吃午餐的学生数量。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>students = [1,1,0,0], sandwiches = [0,1,0,1]\n",
    "<b>输出：</b>0<strong> \n",
    "解释：</strong>\n",
    "- 最前面的学生放弃最顶上的三明治，并回到队列的末尾，学生队列变为 students = [1,0,0,1]。\n",
    "- 最前面的学生放弃最顶上的三明治，并回到队列的末尾，学生队列变为 students = [0,0,1,1]。\n",
    "- 最前面的学生拿走最顶上的三明治，剩余学生队列为 students = [0,1,1]，三明治栈为 sandwiches = [1,0,1]。\n",
    "- 最前面的学生放弃最顶上的三明治，并回到队列的末尾，学生队列变为 students = [1,1,0]。\n",
    "- 最前面的学生拿走最顶上的三明治，剩余学生队列为 students = [1,0]，三明治栈为 sandwiches = [0,1]。\n",
    "- 最前面的学生放弃最顶上的三明治，并回到队列的末尾，学生队列变为 students = [0,1]。\n",
    "- 最前面的学生拿走最顶上的三明治，剩余学生队列为 students = [1]，三明治栈为 sandwiches = [1]。\n",
    "- 最前面的学生拿走最顶上的三明治，剩余学生队列为 students = []，三明治栈为 sandwiches = []。\n",
    "所以所有学生都有三明治吃。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]\n",
    "<b>输出：</b>3\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= students.length, sandwiches.length &lt;= 100</code></li>\n",
    "\t<li><code>students.length == sandwiches.length</code></li>\n",
    "\t<li><code>sandwiches[i]</code> 要么是 <code>0</code> ，要么是 <code>1</code> 。</li>\n",
    "\t<li><code>students[i]</code> 要么是 <code>0</code> ，要么是 <code>1</code> 。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [number-of-students-unable-to-eat-lunch](https://leetcode.cn/problems/number-of-students-unable-to-eat-lunch/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [number-of-students-unable-to-eat-lunch](https://leetcode.cn/problems/number-of-students-unable-to-eat-lunch/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,1,0,0]\\n[0,1,0,1]', '[1,1,1,0,0,1]\\n[1,0,0,0,1,1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        st,sa,n=Counter(students),Counter(sandwiches),len(sandwiches)\n",
    "        if sa==st:\n",
    "            return 0\n",
    "        if sa[1]>st[1]:\n",
    "            a=st[1]+1\n",
    "            b=1\n",
    "        else:\n",
    "            a=st[0]+1\n",
    "            b=0\n",
    "        for i,k in enumerate(sandwiches):\n",
    "            if k==b:\n",
    "                a-=1\n",
    "            if a==0:\n",
    "                return n-i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        cnts = Counter(students)\n",
    "        for sw in sandwiches:\n",
    "            if not cnts[sw]:\n",
    "                break\n",
    "            cnts[sw] -= 1\n",
    "        return cnts[0] + cnts[1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        while  len(students) != 0 and len(sandwiches) != 0:\n",
    "            if sandwiches[0] == 0 and students.count(0) == 0:\n",
    "                break\n",
    "            if sandwiches[0] == 1 and students.count(1) == 0:\n",
    "                break\n",
    "            top = students.pop(0)\n",
    "            if top == sandwiches[0]:\n",
    "                sandwiches.pop(0)\n",
    "            else:\n",
    "                students.append(top)\n",
    "        return len(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        s1 = sum(students)\n",
    "        s0 = len(students) - s1\n",
    "        for x in sandwiches:\n",
    "            if x == 0 and s0:\n",
    "                s0 -= 1\n",
    "            elif x == 1 and s1:\n",
    "                s1 -= 1\n",
    "            else:\n",
    "                break\n",
    "        return s0 + s1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        s1 = sum(students)\n",
    "        s0 = len(students) - s1\n",
    "        for x in sandwiches:\n",
    "            if x == 0 and s0:\n",
    "                s0 -= 1\n",
    "            elif x == 1 and s1:\n",
    "                s1 -= 1\n",
    "            else:\n",
    "                break\n",
    "        return s0 + s1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        student_mapping = {}.fromkeys([0, 1], 0)\n",
    "        for student in students:\n",
    "            student_mapping[student] += 1\n",
    "\n",
    "        for row in sandwiches:\n",
    "            if student_mapping[row] == 0:\n",
    "                break\n",
    "            else:\n",
    "                student_mapping[row] -= 1\n",
    "        return sum(student_mapping.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        times=0\n",
    "        while times<len(students):\n",
    "            if sandwiches[0]==students[0]:\n",
    "                sandwiches.pop(0)\n",
    "                students.pop(0)\n",
    "                times=0\n",
    "            else:\n",
    "                head=students.pop(0)\n",
    "                students.append(head)\n",
    "                times+=1\n",
    "        return len(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        cnt = Counter(students)\n",
    "        n = len(sandwiches)\n",
    "        for i,v in enumerate(sandwiches):\n",
    "            if cnt[v] > 0:\n",
    "                cnt[v] -= 1\n",
    "            else:\n",
    "                return n - i  # 剩余三明治的数量\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        while 1:\n",
    "            if students[0] == sandwiches[0]:\n",
    "                s1 = students.pop(0)\n",
    "                s2 = sandwiches.pop(0)\n",
    "            else:\n",
    "                s1 = students.pop(0)\n",
    "                students.append(s1)\n",
    "            if len(students) == len(sandwiches) == 0:\n",
    "                return 0\n",
    "            if students.count(students[0]) == len(students) and students[0] != sandwiches[0]:\n",
    "                return len(students)\n",
    "\n",
    "\"\"\"\n",
    "当学生里面只剩一种元素时，且三明治的第一个元素和学生的元素不相等时，学生长度就是答案\n",
    "\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        stack=sandwiches\n",
    "        queue=students\n",
    "        while True and sandwiches:\n",
    "            sandwich=sandwiches[0]\n",
    "            student=students[0]\n",
    "            if sandwich not in students:\n",
    "                break\n",
    "            if sandwich==student:\n",
    "                sandwiches.pop(0)\n",
    "                students.pop(0)\n",
    "            else:\n",
    "                students.pop(0)\n",
    "                students.append(student)\n",
    "\n",
    "        return len(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        # 得要知道学生长度length，且轮换次数turn\n",
    "        # 如果length == turn，那么意味着所有学生都轮过一遍了，终止\n",
    "        turn = 0\n",
    "        while students:\n",
    "            if students[0] == sandwiches[0]:\n",
    "                students.pop(0)\n",
    "                sandwiches.pop(0)\n",
    "                turn = 0\n",
    "            else:\n",
    "                \n",
    "                students.append(students.pop(0))\n",
    "                turn += 1\n",
    "                if turn == len(students):\n",
    "                    return len(students)\n",
    "\n",
    "        return 0\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        for i in sandwiches:\n",
    "            if i in students:\n",
    "                students.remove(i)\n",
    "            else:\n",
    "                break\n",
    "        return len(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        sum1, sum2 = sum(students), sum(sandwiches)\n",
    "        if sum1 == sum2:\n",
    "            return 0\n",
    "        elif sum1 > sum2:\n",
    "            tmp = sum1 - sum2\n",
    "            i = len(sandwiches)-1\n",
    "            res = 0\n",
    "            while i >= 0:\n",
    "                res += 1\n",
    "                if sandwiches[i] == 0:\n",
    "                    tmp -= 1\n",
    "                    if tmp == 0:\n",
    "                        break\n",
    "                i -= 1\n",
    "            return res\n",
    "        else:\n",
    "            tmp = sum2 - sum1\n",
    "            i = len(sandwiches)-1\n",
    "            res = 0\n",
    "            while i >= 0:\n",
    "                res += 1\n",
    "                if sandwiches[i] == 1:\n",
    "                    tmp -= 1\n",
    "                    if tmp == 0:\n",
    "                        break\n",
    "                i -= 1\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        '''\n",
    "        s1 = sum(students)  #喜欢三明治为1的数\n",
    "        s2 = len(students) -  s1 #喜欢三明治为0的数\n",
    "        for x in sandwiches:\n",
    "            if x == 0 and s2:\n",
    "                s2 -= 1\n",
    "            elif x == 1 and s1:\n",
    "                s1 -= 1\n",
    "            else:\n",
    "                break\n",
    "        return s2 + s1\n",
    "        '''\n",
    "        p, q = 0, deque(students)\n",
    "        while 1:  \n",
    "            size = len(q)\n",
    "            for i in range(size):\n",
    "                s = q.popleft()\n",
    "                if s == sandwiches[p]:\n",
    "                    p += 1\n",
    "                else:\n",
    "                    q.append(s)\n",
    "            if len(q) == size:\n",
    "                return size   \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        for i in sandwiches:\n",
    "            if i in students:\n",
    "                students.remove(i)\n",
    "            else:\n",
    "                break\n",
    "        return len(students)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        for sandwich in sandwiches:\n",
    "            if sandwich in students:\n",
    "                students.remove(sandwich)\n",
    "                continue\n",
    "            return len(students)\n",
    "        return 0\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        c = [0, 0] # 学生中0和1的数量\n",
    "        for s in students:\n",
    "            c[s] += 1\n",
    "        for s in sandwiches:\n",
    "            c[s] -= 1\n",
    "            if c[s] == -1:\n",
    "                break\n",
    "        return max(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        cnt = Counter(students)\n",
    "        for v in sandwiches:\n",
    "            if cnt[v] == 0:\n",
    "                return cnt[v ^ 1]\n",
    "            cnt[v] -= 1\n",
    "        return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        if students==sandwiches:\n",
    "            return 0\n",
    "        else:\n",
    "            while sandwiches:\n",
    "                if sandwiches[0] not in students:\n",
    "                    return len(students)\n",
    "                elif students[0]==sandwiches[0]:\n",
    "                    students.pop(0),sandwiches.pop(0)\n",
    "                elif students[0]!=sandwiches[0]:\n",
    "                    temp=students[0]\n",
    "                    students[:-1]=students[1:]\n",
    "                    students[-1]=temp\n",
    "            return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        while 1:\n",
    "            if students[0] == sandwiches[0]:\n",
    "                s1 = students.pop(0)\n",
    "                s2 = sandwiches.pop(0)\n",
    "            else:\n",
    "                s1 = students.pop(0)\n",
    "                students.append(s1)\n",
    "            if len(students) == len(sandwiches) == 0:\n",
    "                return 0\n",
    "            if students.count(students[0]) == len(students) and students[0] != sandwiches[0]:\n",
    "                return len(students)\n",
    "\n",
    "\"\"\"\n",
    "当学生里面只剩一种元素时，且三明治的第一个元素和学生的元素不相等时，学生长度就是答案\n",
    "\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        while students and sandwiches and sandwiches[0] in students:\n",
    "            if students[0] == sandwiches[0]:\n",
    "                students.pop(0)\n",
    "                sandwiches.pop(0)\n",
    "            else:\n",
    "                a = students[0]\n",
    "                students.pop(0)\n",
    "                students.append(a)\n",
    "        return len(students)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        s1 = sum(students)\n",
    "        s0 = len(students) - s1\n",
    "        for x in sandwiches:\n",
    "            if x == 0 and s0:\n",
    "                s0 -= 1\n",
    "            elif x == 1 and s1:\n",
    "                s1 -= 1\n",
    "            else:\n",
    "                break\n",
    "        return s0 + s1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        i = 0\n",
    "        while students:\n",
    "            first = students.pop(0)\n",
    "            if first != sandwiches[0]:\n",
    "                students.append(first)\n",
    "                i += 1\n",
    "            else:\n",
    "                sandwiches.pop(0)\n",
    "                i = 0\n",
    "            if i == len(students):\n",
    "                break\n",
    "        return len(students)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        # no s0 but sandwiches[0] == 0\n",
    "        # no s1 but sandwiches[0] == 1\n",
    "        s1 = sum(students)\n",
    "        s0 = len(students) - s1\n",
    "        for sandwich in sandwiches:\n",
    "            if sandwich == 0 and s0:\n",
    "                s0 -= 1\n",
    "            elif sandwich == 1 and s1:\n",
    "                s1 -= 1\n",
    "            else:\n",
    "                break\n",
    "\n",
    "        return s0 + s1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        cnt = 0\n",
    "        while students:\n",
    "            student = students.pop(0)\n",
    "            if student == sandwiches[0]:\n",
    "                sandwiches.pop(0)\n",
    "                cnt = 0\n",
    "            else:\n",
    "                students.append(student)\n",
    "                cnt += 1\n",
    "            if cnt == len(students): return len(students)\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        #tar = sandwiches[-1]\n",
    "        j = len(students)\n",
    "        while j >=0:\n",
    "            l = len(students)\n",
    "            for j in range(len(students)):\n",
    "                tar = sandwiches[0]\n",
    "                if students[j] == tar:\n",
    "                    students.pop(j)\n",
    "                    sandwiches.pop(0)\n",
    "                    break\n",
    "            if len(students) == l:\n",
    "                return len(students)\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        while (not (len(list(set(students))) == 1 and students[0] != sandwiches[0])) and len(students) != 0:\n",
    "            if students[0] == sandwiches[0]:\n",
    "                students.pop(0)\n",
    "                sandwiches.pop(0)\n",
    "            else:\n",
    "                a = students.pop(0)\n",
    "                students.append(a)\n",
    "        return len(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        ans = 0\n",
    "        while len(students) != 0:\n",
    "            if students[0] == sandwiches[0]:\n",
    "                del students[0]\n",
    "                del sandwiches[0]\n",
    "            else:\n",
    "                students.append(students[0])\n",
    "                students.pop(0)\n",
    "            b = set(students)\n",
    "            if (len(b)==1) and (students[0]!=sandwiches[0]):\n",
    "                return len(students)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        count = 1000000\n",
    "        while students and count:\n",
    "            if students[0] == sandwiches[0]:\n",
    "                students.pop(0)\n",
    "                sandwiches.pop(0)\n",
    "            else:\n",
    "                a = students[0]\n",
    "                students.pop(0)\n",
    "                students.append(a)\n",
    "            count -= 1\n",
    "        return len(students)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        cnt = Counter(students)\n",
    "        for stu in sandwiches:\n",
    "            if cnt[stu] <= 0:\n",
    "                break\n",
    "            else:\n",
    "                cnt[stu] -= 1\n",
    "        res_cnt = cnt[0] + cnt[1]\n",
    "        return res_cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        for san in sandwiches:\n",
    "            # san 有顺序的喜欢\n",
    "            ori_len = len(students)\n",
    "            for i, stu in enumerate(students):\n",
    "                if stu == san:\n",
    "                    students = students[1:]\n",
    "                    break\n",
    "                else:\n",
    "                    students = students[1:] + [stu]\n",
    "                if ori_len == (i + 1):\n",
    "                    return ori_len\n",
    "        return 0\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        i = 0\n",
    "        n = len(students) - 1\n",
    "        while i < n:\n",
    "            if students[0] == sandwiches[0]:\n",
    "                students.pop(0)\n",
    "                sandwiches.pop(0)\n",
    "            else:\n",
    "                students.append(students[0])\n",
    "                students.pop(0)\n",
    "            if len(sandwiches) == 0 or sandwiches[0] not in students:\n",
    "                break\n",
    "        return len(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        s1 = sum(students)\n",
    "        s0 = len(students) - s1\n",
    "\n",
    "        for v in sandwiches:\n",
    "            if v == 0 and s0:\n",
    "                s0 -= 1\n",
    "            elif v == 1 and s1:\n",
    "                s1 -= 1\n",
    "            else:\n",
    "                break\n",
    "        return s0 + s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        s1 = sum(students)\n",
    "        s0 = len(students) - s1\n",
    "        for x in sandwiches:\n",
    "            if x == 0 and s0:\n",
    "                s0 -= 1\n",
    "            elif x == 1 and s1:\n",
    "                s1 -= 1\n",
    "            else:\n",
    "                break\n",
    "        return s0 + s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def all_n(self, List, x):\n",
    "        all_int = True\n",
    "        for i in List:\n",
    "            if i != x: \n",
    "                all_int = False\n",
    "        return all_int\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:#不用self.student的原因是因为这个方法中已经有了参数student所以不用引用属性，而在这个类的其他方法中需要使用self.来引用属性减少变量的数量\n",
    "        while students:\n",
    "            if self.all_n(students,1) and sandwiches[0] == 0:\n",
    "                return len(students)\n",
    "            elif self.all_n(students,0) and sandwiches[0] == 1:\n",
    "                return len(students)\n",
    "            elif students[0] == sandwiches[0]:\n",
    "                students.pop(0)\n",
    "                sandwiches.pop(0)\n",
    "            elif students[0] != sandwiches[0]:\n",
    "                m = students[0]\n",
    "                students[0:] = students[1:]\n",
    "                students.append(m)\n",
    "        return len(students)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "    \n",
    "        n = len(students)\n",
    "        start1, start2, i = 0,0,0\n",
    "        \n",
    "        while start2 < n and sandwiches[start2] in students[start1:]:\n",
    "            if start2 < n and sandwiches[start2] == students[start1]:\n",
    "                start1 += 1\n",
    "                start2 += 1\n",
    "            else:\n",
    "                students.append(students[start1])\n",
    "                start1 += 1\n",
    "                \n",
    "        return n - start2\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        n = len(sandwiches)\n",
    "\n",
    "        for i in range(n):\n",
    "            curCount = len(sandwiches)\n",
    "            sandwich = sandwiches[0]\n",
    "            for j in range(curCount):\n",
    "                student = students.pop(0)\n",
    "                if student == sandwich:\n",
    "                    sandwiches.pop(0)\n",
    "                    break\n",
    "                else:\n",
    "                    students.append(student)\n",
    "            afterCount = len(sandwiches)\n",
    "            if afterCount==curCount:\n",
    "                return afterCount\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        s1 = sum(students)\n",
    "        s0 = len(students) - s1\n",
    "        for x in sandwiches:\n",
    "            if x == 0 and s0:\n",
    "                s0 -= 1\n",
    "            elif x == 1 and s1:\n",
    "                s1 -= 1\n",
    "            else:\n",
    "                break\n",
    "        return s0 + s1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        if students==sandwiches:\n",
    "            return 0\n",
    "        else:\n",
    "            while sandwiches:\n",
    "                if sandwiches[0] not in students:\n",
    "                    return len(students)\n",
    "                elif students[0]==sandwiches[0]:\n",
    "                    students.pop(0),sandwiches.pop(0)\n",
    "                elif students[0]!=sandwiches[0]:\n",
    "                    temp=students[0]\n",
    "                    students[:-1]=students[1:]\n",
    "                    students[-1]=temp\n",
    "            return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        # 终止条件：students[0] 不在sandwiches中\n",
    "        while sandwiches and sandwiches[0] in students:\n",
    "            if students[0] == sandwiches[0]:\n",
    "                students.pop(0)\n",
    "                sandwiches.pop(0)\n",
    "            else:\n",
    "                temp = students.pop(0)\n",
    "                students.append(temp)\n",
    "        return len(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        for i in range(len(sandwiches)):\n",
    "            if sandwiches[i] in students:\n",
    "                students.remove(sandwiches[i])\n",
    "            else:\n",
    "                return len(students)\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import deque\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        students1 = deque(students)\n",
    "        sandwiches1 = deque(sandwiches)\n",
    "        while len(sandwiches1) != 0 and sandwiches1[0] in students1:\n",
    "            if students1[0] != sandwiches1[0]:\n",
    "                students1.append(students1.popleft())\n",
    "                \n",
    "            else:\n",
    "                students1.popleft()\n",
    "                \n",
    "                sandwiches1.popleft()\n",
    "        return len(students1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        while len(students) > 0:\n",
    "            if len(Counter(students).keys()) == 1 and students[0] != sandwiches[0]:\n",
    "                return len(students)\n",
    "            else:\n",
    "                if students[0] == sandwiches[0]:\n",
    "                    students.pop(0)\n",
    "                    sandwiches.pop(0)\n",
    "                else:\n",
    "                    students = students[1:] + students[:1]\n",
    "        return len(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        student = deque()\n",
    "        for i in range(len(students)):\n",
    "            student.append(students[i])\n",
    "\n",
    "        stack = []\n",
    "        for i in range(len(sandwiches)):\n",
    "            stack.append(sandwiches[-1])\n",
    "            sandwiches.pop()\n",
    "        flag = len(student)*4\n",
    "\n",
    "        while len(stack)>0:\n",
    "            if student[0]==stack[-1]:\n",
    "                student.popleft()\n",
    "                stack.pop()\n",
    "            elif student[0]!=stack[-1]:\n",
    "                a = student.popleft()\n",
    "                student.append(a)\n",
    "            \n",
    "            flag-=1\n",
    "            if flag==0:\n",
    "                return len(stack)\n",
    "\n",
    "        return len(stack)\n",
    "\n",
    "\n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        cnt = Counter(students)\n",
    "        for v in sandwiches:\n",
    "            if cnt[v] == 0:\n",
    "                return cnt[v ^ 1]\n",
    "            cnt[v] -= 1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        head = 0\n",
    "        tail = len(students) - 1\n",
    "        index =0\n",
    "        gg = 0\n",
    "        while head <= tail:\n",
    "            if sandwiches[index] == students[index]:\n",
    "                head += 1\n",
    "                index += 1\n",
    "            else:\n",
    "                students.append(students.pop(index))\n",
    "                head += 1\n",
    "                tail += 1\n",
    "                gg += 1\n",
    "                if gg > 10000:\n",
    "                    return tail - head + 1\n",
    "\n",
    "        if head - tail == 1:\n",
    "            return 0\n",
    "        \n",
    "        return tail - head + 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        len_one = len(students)\n",
    "        flag = []\n",
    "        while True:\n",
    "            for i in range(len(students)):\n",
    "                if students[0] == sandwiches[0]:\n",
    "                    students.pop(0)\n",
    "                    sandwiches.pop(0)\n",
    "                    break\n",
    "                else:\n",
    "                    students = students[1:] + students[0:1]\n",
    "            flag.append(len(students))\n",
    "            if len(flag) >= len(students) and flag[-1] == flag[-2]:\n",
    "                return len(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def all_n(self, List, x):\n",
    "        all_int = True\n",
    "        for i in List:\n",
    "            if i != x: \n",
    "                all_int = False\n",
    "        return all_int\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:#不用self.student的原因是因为这个方法中已经有了参数student所以不用引用属性，而在这个类的其他方法中需要使用self.来引用属性减少变量的数量\n",
    "        while students:\n",
    "            if self.all_n(students,1) and sandwiches[0] == 0:\n",
    "                return len(students)\n",
    "            elif self.all_n(students,0) and sandwiches[0] == 1:\n",
    "                return len(students)\n",
    "            elif students[0] == sandwiches[0]:\n",
    "                students.pop(0)\n",
    "                sandwiches.pop(0)\n",
    "            elif students[0] != sandwiches[0]:\n",
    "                m = students[0]\n",
    "                students[0:] = students[1:]\n",
    "                students.append(m)\n",
    "        return len(students)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        i=j=t=0\n",
    "        n=len(sandwiches)\n",
    "        while t<n:\n",
    "            if students[i]==sandwiches[j]:\n",
    "                j+=1\n",
    "                n-=1\n",
    "                t=0\n",
    "            else:\n",
    "                students.append(students[i])\n",
    "                t+=1\n",
    "            i+=1\n",
    "        return n\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        student = deque()\n",
    "        for i in range(len(students)):\n",
    "            student.append(students[i])\n",
    "\n",
    "        stack = []\n",
    "        for i in range(len(sandwiches)):\n",
    "            stack.append(sandwiches[-1])\n",
    "            sandwiches.pop()\n",
    "        flag = 0\n",
    "\n",
    "        while len(stack)>0:\n",
    "            if student[0]==stack[-1]:\n",
    "                student.popleft()\n",
    "                stack.pop()\n",
    "            elif student[0]!=stack[-1]:\n",
    "                a = student.popleft()\n",
    "                student.append(a)\n",
    "            \n",
    "            flag+=1\n",
    "            if flag==300:\n",
    "                return len(stack)\n",
    "\n",
    "        return len(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        for sandwich in sandwiches:\n",
    "            if sandwich in students:\n",
    "                students.pop(students.index(sandwich))\n",
    "            else:\n",
    "                return len(students)\n",
    "\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        food = Counter(students)\n",
    "        for i, x in enumerate(sandwiches):\n",
    "            if food[x] == 0:\n",
    "                return len(sandwiches)-i\n",
    "            food[x] -= 1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        st0 = students.count(0)\n",
    "        st1 = students.count(1)\n",
    "\n",
    "        n = len(sandwiches)\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            if sandwiches[i] == 0:\n",
    "                if st0 == 0:\n",
    "                    return st1\n",
    "                else:\n",
    "                    st0 -= 1\n",
    "            else:\n",
    "                if st1 == 0:\n",
    "                    return st0\n",
    "                else:\n",
    "                    st1 -= 1  \n",
    "            i += 1   \n",
    "        return st0 + st1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        s1 = sum(students)\n",
    "        s0 = len(students) - s1\n",
    "        \n",
    "        for i in sandwiches:\n",
    "            if i == 0 and s0:\n",
    "                s0 -= 1\n",
    "            elif i == 1 and s1:\n",
    "                s1 -= 1\n",
    "            else:\n",
    "                break\n",
    "\n",
    "        return s0 + s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        cnts = Counter(students)\n",
    "        for i,x in enumerate(sandwiches):\n",
    "            if cnts[x] == 0:\n",
    "                return len(sandwiches)-i\n",
    "            cnts[x] -= 1\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        for san in sandwiches:\n",
    "            # san 有顺序的喜欢\n",
    "            ori_len = len(students)\n",
    "            for i, stu in enumerate(students):\n",
    "                if stu == san:\n",
    "                    students = students[1:]\n",
    "                    break\n",
    "                else:\n",
    "                    students = students[1:] + [stu]\n",
    "                if ori_len == (i + 1):\n",
    "                    return ori_len\n",
    "        return 0\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        for sandwich in sandwiches:\n",
    "            if sandwich in students:\n",
    "                students.pop(students.index(sandwich))\n",
    "            else:\n",
    "                return len(students)       \n",
    "        return 0 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        cnt = [students.count(0), students.count(1)]\n",
    "        for c in sandwiches:\n",
    "            cnt[c] -= 1\n",
    "            if cnt[0] < 0 or cnt[1] < 0:\n",
    "                return sum(cnt) + 1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        for i in sandwiches:\n",
    "            if i in students:\n",
    "                students.remove(i)\n",
    "            else:\n",
    "                break\n",
    "        return len(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        students = deque(students)\n",
    "        sandwiches.reverse()\n",
    "        while True:\n",
    "            n = len(sandwiches)\n",
    "            m = len(students)\n",
    "            for i in range(m):\n",
    "                cur = students.popleft()\n",
    "                if cur == sandwiches[-1]:\n",
    "                    sandwiches.pop()\n",
    "                else:\n",
    "                    students.append(cur)\n",
    "            if n == len(sandwiches):\n",
    "                break\n",
    "        return len(sandwiches)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        for sandwich in sandwiches:\n",
    "            if sandwich in students:\n",
    "                students.pop(students.index(sandwich))\n",
    "            else:\n",
    "                return len(students)       \n",
    "        return 0 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        sandwiches.reverse()\n",
    "        freq = Counter(students)\n",
    "        while sandwiches and freq[sandwiches[-1]] > 0:\n",
    "            freq[sandwiches[-1]] -= 1\n",
    "            sandwiches.pop()\n",
    "        return len(sandwiches)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        while students and sandwiches and sandwiches[0] in students:\n",
    "            if students[0] == sandwiches[0]:\n",
    "                del students[0]\n",
    "                del sandwiches[0]\n",
    "            else:\n",
    "                students.append(students.pop(0))\n",
    "        return len(students)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        student = deque()\n",
    "        for i in range(len(students)):\n",
    "            student.append(students[i])\n",
    "\n",
    "        stack = []\n",
    "        for i in range(len(sandwiches)):\n",
    "            stack.append(sandwiches[-1])\n",
    "            sandwiches.pop()\n",
    "        flag = len(student)*4\n",
    "\n",
    "        while len(stack)>0:\n",
    "            if student[0]==stack[-1]:\n",
    "                student.popleft()\n",
    "                stack.pop()\n",
    "            elif student[0]!=stack[-1]:\n",
    "                a = student.popleft()\n",
    "                student.append(a)\n",
    "            \n",
    "            flag-=1\n",
    "            if flag==0:\n",
    "                return len(stack)\n",
    "\n",
    "        return len(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        cnt = Counter(students)\n",
    "        for v in sandwiches:\n",
    "            if cnt[v] == 0:\n",
    "                return cnt[v ^ 1]\n",
    "            cnt[v] -= 1\n",
    "        return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        s1 = sum(students)\n",
    "        s0 = len(students) - s1\n",
    "        for x in sandwiches:\n",
    "            if x == 0 and s0:\n",
    "                s0 -= 1\n",
    "            elif x == 1 and s1:\n",
    "                s1 -= 1\n",
    "            else:\n",
    "                break\n",
    "        return s0 + s1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        for i in sandwiches:\n",
    "            if i in students:\n",
    "                students.remove(i)\n",
    "            else:\n",
    "                break\n",
    "        return len(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        cnt = Counter(students)\n",
    "        for i in range(len(sandwiches)):\n",
    "            cnt[sandwiches[i]] -= 1\n",
    "            if cnt[sandwiches[i]] < 0:\n",
    "                return len(sandwiches) - i\n",
    "        \n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        for i in sandwiches:\n",
    "            if i in students:\n",
    "                students.remove(i)\n",
    "            else:\n",
    "                break\n",
    "        return len(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], \n",
    "                      sandwiches: List[int]) -> int:\n",
    "        std = deque(students)\n",
    "        sandwiches.reverse()\n",
    "        while len(std) > 0:\n",
    "            n = len(std)\n",
    "            for i in range(n):\n",
    "                s = std.popleft()\n",
    "                if s == sandwiches[-1]:\n",
    "                    sandwiches.pop()\n",
    "                else:\n",
    "                    std.append(s)\n",
    "            if n == len(std):\n",
    "                break\n",
    "        return len(sandwiches)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        n = len(students)\n",
    "        for i in range(n * n):\n",
    "            if students and students[0] == sandwiches[0]:\n",
    "                students.pop(0)\n",
    "                sandwiches.pop(0)\n",
    "            elif students and sandwiches:\n",
    "                students.append(students.pop(0))\n",
    "        return len(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        s1 = sum(students)\n",
    "        s0 = len(students)-s1\n",
    "        for x in sandwiches:\n",
    "            if x==0 and s0:\n",
    "                s0-=1\n",
    "            elif x==1 and s1:\n",
    "                s1-=1\n",
    "            else:\n",
    "                break\n",
    "        return s0+s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:\n",
    "        self.students = students\n",
    "        self.sandwiches = sandwiches\n",
    "        self.q = sandwiches.copy()\n",
    "        for i in self.q:\n",
    "            a = 0\n",
    "            for _ in range(len(self.students)):\n",
    "                if self.students[0] == i:\n",
    "                    self.sandwiches.pop(0)\n",
    "                    self.students.pop(0)\n",
    "                    break\n",
    "                else: \n",
    "                    self.students.append(self.students[0])\n",
    "                    self.students.pop(0)\n",
    "                    a += 1\n",
    "                if a == len(self.students):\n",
    "                    return len(self.students)\n",
    "        return len(self.students)            \n",
    "                                                                                                                                                                                                                                                                                                                                                                                                                                                           "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
